<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app;

use think\App;
use think\exception\HttpResponseException;
use think\Response;
use think\exception\ValidateException;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class IndexBaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 用户信息
     * @var \think\Request
     */
    protected $userinfo;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 模型实例
     * @var \think\App
     */
    protected $model;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 传入参数
     * @var array
     */
    protected $reqdata = [];

    /**
     * 传入参数
     * @var array
     */
    protected $param = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
        $this->reqdata = $this->app->request->request();
        $this->userinfo = $this->request->userinfo;
        $this->param = $this->app->request->param();
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize(){
        if ($this->request->method() == 'POST') {
            $check = $this->request->checkToken();
            if (false === $check) {
                $this->error('操作失败请重试，重试无效请刷新页面');
            }
        }
    }

    function get_real_ip()
    {
        $ip='未知IP';
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }elseif(!empty($_SERVER['HTTP_X_REAL_IP'])){
            $ip = $_SERVER['HTTP_X_REAL_IP'];
        }elseif(!empty($_SERVER['REMOTE_ADDR'])){
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    protected function success($data=[],$msg='',$time=2){
        $data['token'] = token();
        $res = [
            'code'=>0,
            'data'=>$data,
            'msg'=>$msg,
            'time'=>$time
        ];
        $response = Response::create($res, 'json')->options(['jump_template' => app()->config->get('app.dispatch_success_tmpl')]);
        throw new HttpResponseException($response);
    }
    protected function error($msg='',$code=1,$data=null,$time=2){
        $data['token'] = token();
        $res = [
            'code'=>$code,
            'data'=>$data,
            'msg'=>$msg,
            'time'=>$time
        ];
        $response = Response::create($res, 'json')->options(['jump_template' => app()->config->get('app.dispatch_success_tmpl')]);
        throw new HttpResponseException($response);
    }
}
